Voorbeeld van de functie Dir

Dit voorbeeld maakt gebruik van de functie Dir om te controleren of bepaalde bestanden en mappen bestaan. Op de Macintosh is "HD:" de standaardstationsnaam en worden delen van de padnaam gescheiden door dubbele punten en niet door schuine strepen. Bovendien worden de jokertekens van Microsoft Windows op de Macintosh als geldige tekens in bestandsnamen geaccepteerd. Toch kunt u met de functie MacID bestandsgroepen opgeven.

Dim MijnBestand, MijnPad, MijnNaam
' geeft "WIN.INI" (op Microsoft Windows) als resultaat, als dit bestand bestaat.
MijnBestand = Dir("C:\WINDOWS\WIN.INI")    

' geeft bestandsnaam met opgegeven extensie als resultaat. Als er meer dan ΘΘn bestand met de extensie .ini
' bestaat, is het eerste gevonden bestand het resultaat.
MijnBestand = Dir("C:\WINDOWS\*.INI")

' Geef de opdracht Dir nogmaals zonder argumenten als u het volgende .INI-bestand in dezelfde
' directory als resultaat wilt krijgen.
MyFile = Dir

' Return first *.TXT file with a set hidden attribute.
MyFile = Dir("*.TXT", vbHidden)

' Display the names in C:\ that represent directories.
MyPath = "c:\"    ' Set the path.
MyName = Dir(MyPath, vbDirectory)    ' Retrieve the first entry.
Do While MyName <> ""    ' Start the loop.
    ' Ignore the current directory and the encompassing directory.
    If MyName <> "." And MyName <> ".." Dan
        ' Use bitwise comparison to make sure MyName is a directory.
        If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
            Debug.Print MyName    ' Display entry only if it
        End If    ' it represents a directory.
    End If
    MyName = Dir    ' Get next entry.
Loop